var EnableKey = "www.taosijie.com.chrometool.Enable";
var UrlKey = "www.taosijie.com.chrometool.Url";
var MaxPriceKey = "www.taosijie.com.chrometool.MaxPrice";
var MaxNumberKey = "www.taosijie.com.chrometool.MaxNumber";

// 获取本地存储值
function GetLocalStorageValueString(key){
    if(window.localStorage){
        var value = window.localStorage.getItem(key);
        return value;
    }
    return null;
}

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    switch (request.type) {
        case "autobuy":
            var OpenUrl = request.url;
            var ConfigUrl = GetLocalStorageValueString(UrlKey);

            // 功能开关
            var EnableValue = false;
            if(OpenUrl == ConfigUrl){
                if(GetLocalStorageValueString(EnableKey) == "open"){
                    EnableValue = true;
                }
            }

            // 价格
            var PriceValue = 0.0;
            let PriceTemp = GetLocalStorageValueString(MaxPriceKey);
            if(PriceTemp){
                PriceValue = parseFloat(PriceTemp);
            }
            // 数量
            var NumberValue = 0;
            let NumberTemp = GetLocalStorageValueString(MaxNumberKey);
            if(NumberTemp){
                NumberValue = parseInt(NumberTemp);
            }
            sendResponse({Enable:EnableValue, Price:PriceValue, Number:NumberValue});
            break;
        default: break;
    }
});